<?php
namespace app\backend\controller;

class Home extends Backend {
	
	public function index() {
		
	}
	
	/**
	 * 编辑密码
	 */
	public function edit_password() {
		
		$parameter = \think\Request::instance()->post();
		if(!array_key_exists('action', $parameter) || !is_string($parameter['action'])){
			return $this->disposeJson(1, '未传入指定参数');
		};
		
		switch($parameter['action']){
			
			// 获取页面
			case 'get_page' :
				
				$view = new \think\View();
				
				$page = $view->fetch();
				
				return $this->disposeJson(0, '', array(
					'page' => $page
				));
			
			// 提交保存
			case 'submit' :
			
				$judge_1 = array_key_exists('password_old', $parameter) && is_string($parameter['password_old']) && (6 <= mb_strlen($parameter['password_old'], 'utf8')) && (20 >= mb_strlen($parameter['password_old'], 'utf8')) && (1 === preg_match('/^[0-9A-Za-z]*$/', $parameter['password_old']));
				$judge_2 = array_key_exists('password_new', $parameter) && is_string($parameter['password_new']) && (6 <= mb_strlen($parameter['password_new'], 'utf8')) && (20 >= mb_strlen($parameter['password_new'], 'utf8')) && (1 === preg_match('/^[0-9A-Za-z]*$/', $parameter['password_new']));
				if(!$judge_1 || !$judge_2){
					return $this->disposeJson(1, '参数校验失败');
				};
				
				if($parameter['password_old'] === $parameter['password_new']){
					return $this->disposeJson(1, '新密码不能与旧密码相同');
				};
				
				$ManagerModel = \app\common\model\ManagerModel::get($this->manager_id);
				if(null === $ManagerModel){
					return $this->disposeJson(1, '意外错误，请联系管理员');
				};
				
				$parameter['password_old'] = md5($parameter['password_old'] . \think\Config::get('manager_password_suffix'));
				if($parameter['password_old'] !== $ManagerModel->password){
					return $this->disposeJson(1, '旧密码不匹配');
				};
				
				$ManagerModel->password = md5($parameter['password_new'] . \think\Config::get('manager_password_suffix'));
				$result = $ManagerModel->save();
				if(false == $result){
					return $this->disposeJson(1, '保存数据失败，请联系管理员');
				};
				
				\think\Session::delete('manager_id');
				return $this->disposeJson(0, '操作成功，请重新登陆');
				break;
			
			default:
				return $this->disposeJson(1, '传入参数不正确');
		};
	}
	
	/**
	 * 退出登录
	 */
	public function logout() {
		
		// 清除登录状态
		\think\Session::clear();
		
		return $this->disposeJson(0, '操作成功，即将刷新');
	}
	
}